package com.ccl.iot.managers;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.ccl.iot.publics.Debugger;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.db.DatabaseContext;

import java.util.List;

public abstract class Manager {
    private final static int KMESSAGE_ONCREATE = 1;
    private final static int KMESSAGE_RESUME = 2;
    private final static int KMESSAGE_SUSPEND = 3;

    private static ActivityManager iActivityManager = null;

    public static DatabaseContext DBCONTEXT = new DatabaseContext(UIUtils.getContext(), "databases/");

    private Context iContext = null;

    private boolean iCreating = true;
    private volatile boolean iSuspend = true;
    private Object iSyncLock = new Object();

    public Manager(Context aContext) {
        iContext = aContext;

        if (iContext != null && iActivityManager == null) {
            iActivityManager = (ActivityManager) iContext.getSystemService(Context.ACTIVITY_SERVICE);
        }

//        iHandler.sendEmptyMessage(KMESSAGE_ONCREATE);
    }

    private Handler iHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg != null) {
                if (msg.what == KMESSAGE_ONCREATE) {
                    create();
                } else if (msg.what == KMESSAGE_RESUME) {
                    resume();
                } else if (msg.what == KMESSAGE_SUSPEND) {
                    suspend();
                }
            }
        }
    };

    public Context getContext() {
        return iContext;
    }

    public boolean isBackground(boolean aActivityOnly) {
        if (iActivityManager != null) {
            String tAppName = iContext.getPackageName();

            if (aActivityOnly) {    //Need permission <uses-permission android:name="android.permission.GET_TASKS" />
                List<RunningTaskInfo> tTasks = iActivityManager.getRunningTasks(1);

                if (tTasks != null && tTasks.size() > 0) {
                    return tTasks.get(0).topActivity.getPackageName().equals(tAppName) == false;
                }
            } else {
                List<RunningAppProcessInfo> tProcesses = iActivityManager.getRunningAppProcesses();

                for (RunningAppProcessInfo tProcess : tProcesses) {
                    if (tProcess != null && tProcess.processName.equals(tAppName)) {
                        return tProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
                    }
                }
            }
        }

        return false;
    }

    public void create() {
        iCreating = false;

        Debugger.Error("OnCreate:" + this.getClass().getSimpleName());
    }

    public void onResume() {
        Debugger.Error("OnResume:" + this.getClass().getSimpleName());
    }

    public void resume() {
        synchronized (iSyncLock) {
            if (iSuspend) {
                if (iCreating == false) {
                    iSuspend = false;

                    onResume();
                } else {
                    iHandler.sendEmptyMessage(KMESSAGE_RESUME);
                }
            }
        }
    }

    public void onSuspend() {
        Debugger.Error("OnSuspend:" + this.getClass().getSimpleName());
    }

    public void suspend() {
        synchronized (iSyncLock) {
            if (iSuspend == false) {
                if (iCreating == false) {
                    iSuspend = true;

                    onSuspend();
                } else {
                    iHandler.sendEmptyMessage(KMESSAGE_SUSPEND);
                }
            }
        }
    }

    public boolean isSuspend() {
        return iSuspend;
    }

    public boolean filteError(int aError) {
        return false;
    }
}
